package com.threegroup.web.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.threegroup.common.dao.DishDao;
import com.threegroup.common.dao.OrderDetailDao;
import com.threegroup.common.dao.OrdersDao;
import com.threegroup.common.dto.OrderDetailDTO;
import com.threegroup.common.dto.OrderDetailPageDTO;
import com.threegroup.common.entity.DishEntity;
import com.threegroup.common.entity.OrderDetailEntity;
import com.threegroup.common.utils.AssertUtil;
import com.threegroup.common.utils.Result;
import com.threegroup.common.validator.AssertUtils;
import com.threegroup.common.vo.OrderDetailInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

@Service
public class OrderDetailService {
    //订单明细
    @Resource
    private OrderDetailDao orderDetailDao;
    //订单
    @Resource
    private OrdersDao ordersDao;
    //菜品
    @Resource
    private DishDao dishDao;

    //添加订单明细
    @Transactional(propagation = Propagation.REQUIRED)
    public Result addOrderDetail(OrderDetailDTO orderDetailDTO,Long orderId){

        /**
         * 校验orderId是否存在
         *      存在表明是添加用户订单时添加的订单明细
         *          设置orderId值
         *      不存在表示单独添加的订单明细，orderId是前端orderDetailDTO里传来的
         *          校验orderId值
         *              1.不能为空
         *              2.数据库中存在
         */
        if (null==orderId){
            orderId = orderDetailDTO.getOrderId();
            AssertUtils.isNull(orderId,302);
            AssertUtil.isTrue(ordersDao.selectByPrimaryKey(orderId)==null,"该订单不存在");
        }

        //1.参数校验

        Result result = new Result();
        AssertUtils.isNull(orderDetailDTO,302,"订单详情为空");

        //提取数据
        //BigDecimal amount = orderDetailDTO.getAmount();
        //String dishFlavor = orderDetailDTO.getDishFlavor();
        Long dishId = orderDetailDTO.getDishId();
        String image = orderDetailDTO.getImage();
        String name = orderDetailDTO.getName();
        Integer number = orderDetailDTO.getNumber();
        //Long setmealId = orderDetailDTO.getSetmealId();
        /*  菜品ID
            1.不能为空
            2.数据库中存在
         */
        AssertUtils.isNull(dishId,302);
        DishEntity dishTemp = dishDao.selectByPrimaryKey(dishId);
        AssertUtil.isTrue(dishTemp == null,"该菜品不存在");

        /*数量
            1.不能为空并且非负
         */
        if(null==number||number<=0){
            return null;
        }

        /*
        金额
            1.和菜品ID关联从数据库中查
            2.不能为空
            3.不能为0或为负数
         */
        //BigDecimal amount1 = dishTemp.getPrice();
        BigDecimal amount = dishTemp.getPrice().multiply(BigDecimal.valueOf(number)).divide(new BigDecimal(100));
        AssertUtil.isTrue(null==amount||amount.compareTo(BigDecimal.ZERO)<=0,"订单详情金额有误");


        //设置默认值
        //设置订单明细名的默认值
        name = (dishTemp.getName());

        //插入数据到Entity数据模型
        OrderDetailEntity orderDetailEntity = new OrderDetailEntity();
        orderDetailEntity.setOrderId(orderId);
        orderDetailEntity.setName(name);
        orderDetailEntity.setAmount(amount);
        orderDetailEntity.setNumber(number);
        orderDetailEntity.setDishId(dishId);
        orderDetailEntity.setImage(dishTemp.getImage());

        //执行插入操作
        AssertUtil.isTrue(orderDetailDao.insertSelective(orderDetailEntity)<1,"添加订单明细失败");

        return result;
    }

    //查询该订单明细的订单信息
    public Result selectOrderDetail(Integer id) {
        Result result = new Result();
        //判断ids是否为空
        AssertUtils.isNull(id,"id为空");
        //判断该数据是否存在
        OrderDetailEntity orderDetailEntity = new OrderDetailEntity();
        AssertUtils.isNull(orderDetailEntity = orderDetailDao.selectByPrimaryKey(id.longValue()),"该订单明细记录不存在");

        result.setData(orderDetailEntity);

        return result;
    }

    //查询根据orderId查多个订单明细信息
    public Result selectOrderDetailInfoVo(Long orderId) {

        Result result = new Result();
        //判断ids是否为空
        AssertUtils.isNull(orderId,"OrderId为空");
        //判断该数据是否存在

        List<OrderDetailEntity> orderDetailEntities = orderDetailDao.selectByOrderId(orderId);
        AssertUtils.isNull(orderDetailEntities,"该订单明细记录不存在");

        //新建一个orderDetailInfoVos存放赋值的orderDetailInfoVo
        List<OrderDetailInfoVo> orderDetailInfoVos = new ArrayList<>();
        for (OrderDetailEntity orderDetailEntity : orderDetailEntities) {
            OrderDetailInfoVo orderDetailInfoVo = new OrderDetailInfoVo();
            orderDetailInfoVo.setAmount(orderDetailEntity.getAmount());
            orderDetailInfoVo.setName(orderDetailEntity.getName());
            orderDetailInfoVo.setNumber(orderDetailEntity.getNumber());
            orderDetailInfoVos.add(orderDetailInfoVo);
        }

        result.setData(orderDetailInfoVos);
        return result;
    }

    //分页查询订单明细列表
    public Result selectOrderDetailList(OrderDetailPageDTO orderDetailPageDTO) {

        Result result = new Result();

        String order = orderDetailPageDTO.getOrder();
        Integer page = orderDetailPageDTO.getPage();
        Integer limit = orderDetailPageDTO.getLimit();
        String orderField = orderDetailPageDTO.getOrderField();
        //订单Id
        Long orderId = orderDetailPageDTO.getId();

        // 只允许指定的排序字段和排序方式，防止SQL注入
        String[] orderByArr = {"amount", "id","number"};
        String orderByStr = "";
        if (StringUtils.isNotEmpty(orderField) && Arrays.asList(orderByArr).contains(orderField.toLowerCase())) {
            orderByStr = String.format("%s %s", orderField.toLowerCase(), "asc".equalsIgnoreCase(order) ? "asc" : "desc");
        } else {
            // 默认排序
            orderByStr = "id asc";
        }

        PageHelper.startPage(page, limit, orderByStr);
        List<OrderDetailEntity> orderDetailEntities = orderDetailDao.selectAll(orderId);
        PageInfo<OrderDetailEntity> orderDetailEntitiesPageInfo = new PageInfo<>(orderDetailEntities);

        Map<String,Object> map = new HashMap<>();
        map.put("list",orderDetailEntitiesPageInfo.getList());
        map.put("total",orderDetailEntitiesPageInfo.getTotal());
        result.setData(map);

        return result;
    }
}
